package com.ruoyi.technicianAudit.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.check.CheckEmptyUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.technician.domain.Technician;
import com.ruoyi.technician.mapper.TechnicianMapper;
import com.ruoyi.technicianAudit.domain.TechnicianAvatarAudit;
import com.ruoyi.technicianAudit.service.ITechnicianAvatarAuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 头像审核Controller
 *
 * @author ruoyi
 * @date 2025-04-07
 */
@RestController
@RequestMapping("/technicianAvatarAudit/technicianAvatarAudit")
public class TechnicianAvatarAuditController extends BaseController {
    @Autowired
    private ITechnicianAvatarAuditService technicianAvatarAuditService;
    @Resource
    private TechnicianMapper technicianMapper;

    /**
     * 查询头像审核列表
     */
    @PreAuthorize("@ss.hasPermi('technicianAvatarAudit:technicianAvatarAudit:list')")
    @GetMapping("/list")
    public TableDataInfo list(TechnicianAvatarAudit technicianAvatarAudit) {
        startPage();
        List<TechnicianAvatarAudit> list = technicianAvatarAuditService.selectTechnicianAvatarAuditList(technicianAvatarAudit);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianAvatarAudit avatarAudit : list) {
                String tName = technicianMapper.selectTName(avatarAudit.getTid());
                avatarAudit.setTname(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出头像审核列表
     */
    @PreAuthorize("@ss.hasPermi('technicianAvatarAudit:technicianAvatarAudit:export')")
    @Log(title = "头像审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TechnicianAvatarAudit technicianAvatarAudit) {
        List<TechnicianAvatarAudit> list = technicianAvatarAuditService.selectTechnicianAvatarAuditList(technicianAvatarAudit);
        if (!CheckEmptyUtil.isEmpty(list)) {
            for (TechnicianAvatarAudit avatarAudit : list) {
                String tName = technicianMapper.selectTName(avatarAudit.getTid());
                avatarAudit.setTname(CheckEmptyUtil.isEmpty(tName) ? "" : tName);
            }
        }
        ExcelUtil<TechnicianAvatarAudit> util = new ExcelUtil<TechnicianAvatarAudit>(TechnicianAvatarAudit.class);
        util.exportExcel(response, list, "头像审核数据");
    }

    /**
     * 获取头像审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('technicianAvatarAudit:technicianAvatarAudit:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(technicianAvatarAuditService.selectTechnicianAvatarAuditById(id));
    }

    /**
     * 新增头像审核
     */
    @PreAuthorize("@ss.hasPermi('technicianAvatarAudit:technicianAvatarAudit:add')")
    @Log(title = "头像审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TechnicianAvatarAudit technicianAvatarAudit) {
        return toAjax(technicianAvatarAuditService.insertTechnicianAvatarAudit(technicianAvatarAudit));
    }

    /**
     * 修改头像审核
     */
    @PreAuthorize("@ss.hasPermi('technicianAvatarAudit:technicianAvatarAudit:edit')")
    @Log(title = "头像审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TechnicianAvatarAudit technicianAvatarAudit) {
        if (technicianAvatarAudit.getAuditStatus() == 1) {
            Technician technician = technicianMapper.selectTechnicianById(technicianAvatarAudit.getTid());
            technician.setTechnicianAvatar(technicianAvatarAudit.getNewAvatar());
            technicianMapper.updateTechnician(technician);
        }
        return toAjax(technicianAvatarAuditService.updateTechnicianAvatarAudit(technicianAvatarAudit));
    }

    /**
     * 删除头像审核
     */
    @PreAuthorize("@ss.hasPermi('technicianAvatarAudit:technicianAvatarAudit:remove')")
    @Log(title = "头像审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(technicianAvatarAuditService.deleteTechnicianAvatarAuditByIds(ids));
    }
}
